﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MVCCentral.Framework.Mvc.Fyneworks
{
    class RatedBuilder : FormBuilder
    {
        private double Avg;
        private int? Votes;
        private string CurrentlyRated;

        public RatedBuilder(double avg, int? votes, string currentlyRated)
            : base("StarsRated")
        {
            this.Avg = avg;
            this.Votes = votes;
            this.CurrentlyRated = currentlyRated;
        }

        public override void OpenForm()
        {
            Form.OpenForm = "<form action=\"\" method=\"post\" id=\"" + Form.FormName + "\">" + Environment.NewLine;
        }

        public override void GenerateInputs()
        {
            TagBuilder tag = null;
            int counter = 0;

            for (int i = 0; i < 20; i++)
            {
                tag = new TagBuilder("input");
                tag.MergeAttribute("type", "radio");
                tag.MergeAttribute("name", "rating");
                tag.MergeAttribute("class", "star {split:4}");
                tag.MergeAttribute("value", (i + 1).ToString());
                if (MakeChecked(Avg, i, ref counter))
                    tag.MergeAttribute("checked", "checked");
                Form.Tags.Add(tag.ToString() + Environment.NewLine);
            }
        }

        bool MakeChecked(double reference, int i, ref int counter)
        {
            double lower = 0;
            double higher = 0;
            bool result = false;

            if ((double)i % 4 == 0)
            {
                lower = (0.0 + counter);
                higher = (0.25 + counter);
            }
            if ((double)i % 4 == 1)
            {
                lower = (0.25 + counter);
                higher = (0.50 + counter);
            }
            if ((double)i % 4 == 2)
            {
                lower = (0.50 + counter);
                higher = (0.75 + counter);
            }
            if ((double)i % 4 == 3)
            {
                lower = (0.75 + counter);
                higher = counter + 1;
                counter++;
            }
            if (reference > lower && reference <= higher)
            {
                result = true;
            }
            return result;
        }

        public override void EndForm()
        {
            Form.EndForm = "</form>" + Environment.NewLine;
        }

        public override void OpenDiv()
        {
            string openDiv = string.Empty;
            openDiv = "<span id=\"rated\">" + Environment.NewLine;
            //openDiv = "<div style=\"float:left;\">" + Environment.NewLine;
            Form.OpenDiv = openDiv;
        }

        public override void CloseDiv()
        {
            string div = string.Empty;
            div = Environment.NewLine + "<span id=\"currentlyrated\" style=\"float:left;padding-left:20px;\">" + Environment.NewLine;
            if (Avg > 0)
            {
                if (!string.IsNullOrEmpty(CurrentlyRated))
                {
                    //div += "Currently rated " + Avg.ToString("f") + " by " + Votes + " people";
                    div += string.Format(CurrentlyRated, Avg.ToString("f"), Votes);
                    //div += FormatString(CurrentlyRated);
                }
            }
            else
            {
                div += "<span style=\"color:red\">Not yet rated.  Be the first to rate this article!</span>" + Environment.NewLine;
            }
            div += "</span>" + Environment.NewLine;
            //div += "</div>" + Environment.NewLine;
            div += "</span>" + Environment.NewLine + "<div style=\"clear:both\"></div>" + Environment.NewLine;

            Form.CloseDiv = div;
        }
    }
}
